﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simulator
{
	
    class Node
    {
    	private static string hello = "Hello";
        private Manager manager;
        public int ID;
		private HashSet<int> nodesIHear = new HashSet<int>();
        private HashSet<int> unAckedNodes = new HashSet<int>();
        public double X, Y; // for use by the manager only

        private bool[][] connections;

        private int?[] clusterHeads;

        private int myClusterHead;

        private int power;

        public Node(Manager manager, int ID, int numOfNodes)
        {
            this.manager = manager;
            this.ID = ID;
            connections = new bool[numOfNodes][];
            for (int i = 0; i < numOfNodes; i++)
            {
                connections[i] = new bool[numOfNodes];
            }

            clusterHeads=new int?[numOfNodes];
            if (clusterHeads[0]!=null||connections[0][0]) //TODO: remove this
                throw new Exception("init array");
            
        }

        public void clusterize1()
        {
            manager.probe(ID,unAckedNodes, null);
            unAckedNodes.Clear();
        }

        public void clusterize2()
        {
            myClusterHead = ID;
            for (int i = clusterHeads.Length; i>0 ; --i)
            {
                if (clusterHeads[i] == i)
                    myClusterHead = i;
            }
            manager.probe(ID, unAckedNodes, myClusterHead);
        }


        public void messageRecieved(Message message)
        {
			if (!nodesIHear.Contains(message.source))
			{
				nodesIHear.Add(message.source);
			}
            throw new NotImplementedException();
        }

        public void probed(int source, HashSet<int> heardNodes, int? clusterHead)
        {
            nodesIHear.Add(source);
            unAckedNodes.Add(source);
            foreach (int i in heardNodes)
            {
                connections[source][i] = true;
            }
            if (clusterHead != null)
                clusterHeads[source] = clusterHead;
        }
    }
}
